﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：NewsManage.cs
  //文件功能描述：资讯管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class NewsManage
    {
        private readonly DALNews dalNews = new DALNews();
        private readonly DALNewsCategories dalCategory = new DALNewsCategories();

        #region "更新资讯的点击数；by 赵国伟 2012-03-18"
        /// <summary>
        /// 更新资讯的点击数；
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public int UpdateClicks(int newsId)
        {
            return dalNews.UpdateClicks(newsId);
        }
        #endregion

        #region "保存资讯类别；by 赵国伟 2012-03-29"
        /// <summary>
        /// 保存资讯类别；
        /// </summary>
        /// <returns></returns>
        public ReturnValue SaveCategory(NewsCategories categoryInfo)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"保存失败！");
            if (categoryInfo == null)
            {
                ret.Message = "类别信息不能为空！";
            }
            else
            {
                if (categoryInfo.CategoryID <= 0)
                {
                    #region "添加的情况"
                    if (dalCategory.QueryList(1, "CategoryName='" + categoryInfo.CategoryName + "' ", "") != null)
                    {
                        ret.Message = "类别名称不能重复！";
                    }
                    else
                    {
                        categoryInfo.CreateDate = DateTime.Now;
                        if (dalCategory.Add(categoryInfo) > 0)
                        {
                            ret.Result = ResultType.Success;
                            ret.Message = "保存成功！";
                        }
                    }
                    #endregion
                }
                else
                {
                    #region "修改的情况"
                    if (dalCategory.QueryList(1, "(CategoryName='" + categoryInfo.CategoryName + "' )  and CategoryID <>" + categoryInfo.CategoryID + "", "") != null)
                    {
                        ret.Message = "类别名称不能重复！";
                    }
                    else
                    {
                        NewsCategories oldInfo = dalCategory.GetModel(categoryInfo.CategoryID);
                        categoryInfo.CreateDate = oldInfo.CreateDate;
                        if (dalCategory.Update(categoryInfo) > 0)
                        {
                            ret.Result = ResultType.Success;
                            ret.Message = "保存成功！";
                        }
                    }
                    #endregion
                }
            }
            return ret;
        }
        #endregion

        #region "查询指定的一条资讯类别信息；by 赵国伟 2012-03-29"
        /// <summary>
        /// 查询指定的一条资讯类别信息；
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public NewsCategories GetCategoryInfo(int categoryId)
        {
            return dalCategory.GetModel(categoryId);
        }
        #endregion

        #region "查询所有的类别信息； by 赵国伟 2012-03-29"
        /// <summary>
        /// 查询所有的类别信息；
        /// </summary>
        /// <returns></returns>
        public List<NewsCategories> QueryCategories(bool IsAvailable)
        {
            string cond = "Status<>'" + Status.已删除.ToString() + "' ";
            if (IsAvailable)
                cond += " and Status='" + Status.正用.ToString() + "'";
            return dalCategory.QueryList(cond, "CategoryID asc ");
        }
        #endregion

        #region "获得新闻类别的下拉选项 by 赵国伟 2012-03-24"
        /// <summary>
        /// 获得新闻类别的下拉选项
        /// </summary>
        /// <returns></returns>
        public static string GetCategoriesToSelectOption(bool IsAvailable, string defaultValue)
        {
            NewsManage bllNews = new NewsManage();
            List<NewsCategories> listOfData = bllNews.QueryCategories(IsAvailable);
            StringBuilder sb = new StringBuilder(500);
            if (listOfData != null)
            {
                foreach (NewsCategories datainfo in listOfData)
                {
                    sb.Append("<option value=\"");
                    sb.Append(datainfo.CategoryID);
                    sb.Append("\" ");
                    if (defaultValue == datainfo.CategoryID.ToString())
                    {
                        sb.Append(" Selected ");
                    }
                    sb.Append(">");
                    sb.Append(datainfo.CategoryName);
                    sb.Append("</option>");
                }
            }
            return sb.ToString();
        }
        #endregion

        #region "删除指定的类别； by 赵国伟 2012-03-29"
        /// <summary>
        /// 删除指定的类别；
        /// </summary>
        /// <param name="CategoryID"></param>
        /// <returns></returns>
        public int DeleteCategories(int CategoryID)
        {
            return dalCategory.Delete(CategoryID);
        }
        #endregion

        #region "查询所有的资讯类别信息并存为Hash结构； by 赵国伟 2012-03-31"
        /// <summary>
        /// 查询所有的资讯类别信息并存为Hash结构；
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> QueryCategoryToHash()
        {
            Dictionary<int, string> dict = new Dictionary<int, string>();
            List<NewsCategories> listOfCategores = QueryCategories(false);
            if (listOfCategores != null)
            {
                foreach (NewsCategories c in listOfCategores)
                {
                    dict.Add(c.CategoryID, c.CategoryName);
                }
            }
            return dict;
        }
        #endregion


        #region "保存新闻信息； by 赵国伟 2012-03-31"
        /// <summary>
        /// 保存资讯信息；
        /// </summary>
        /// <param name="newsInfo"></param>
        /// <returns></returns>
        public ReturnValue SaveNews(News newsInfo)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败！");
            if (newsInfo == null)
            {
                ret.Message = "类别信息不能为空！";
            }
            else
            {
                if (newsInfo.NewsID <= 0)
                {
                    #region "添加的情况"
                    newsInfo.CreateDate = DateTime.Now;
                    newsInfo.LastModifyDate = DateTime.Now;
                    if (dalNews.Add(newsInfo) > 0)
                    {
                        ret.Result = ResultType.Success;
                        ret.Message = "保存成功！";
                    }
                    #endregion
                }
                else
                {
                    #region "修改的情况"
                    News oldInfo = dalNews.GetModel(newsInfo.NewsID);
                    newsInfo.CreateDate = oldInfo.CreateDate;
                    newsInfo.LastModifyDate = DateTime.Now;
                    newsInfo.Clicks = oldInfo.Clicks;
                    if (dalNews.Update(newsInfo) > 0 )
                    {
                        ret.Result = ResultType.Success;
                        ret.Message = "保存成功！";
                    }
                    #endregion
                }
            }
            return ret;
        }
        #endregion

        #region "查询一条指定的资讯信息； by 赵国伟 2012-03-31"
        /// <summary>
        /// 查询一条指定的资讯信息；
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public NewsInfo GetNews(int newsId)
        {
            NewsInfo d = null;
            News news = dalNews.GetModel(newsId);
            if (news != null)
            {
                d = new NewsInfo();
                d.news = news;
                d.CategoryName = DALHelper.GetFieldValue<NewsCategories>(news.CategoryID, "CategoryName");
            }
            return d;
        }
        #endregion

        #region "删除一条指定的资讯信息； by 赵国伟 2012-03-31"
        /// <summary>
        /// 删除一条指定的资讯信息；
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public ReturnValue DeleteNews(int newsId)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败！");
            if (dalNews.Delete(newsId) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }
            return ret;
        }
        #endregion


        #region "多条件分页查询资讯信息； by 赵国伟 2012-03-31"
        /// <summary>
        /// 多条件分页查询资讯信息；
        /// </summary>
        /// <param name="qo"></param>
        /// <returns></returns>
        public List<NewsInfo> QueryNewsAsPage(QueryNewsAsPage qo)
        {
            string cond = "1=1";
            if (qo.CategoryID > 0)
                cond += " and CategoryID =" + qo.CategoryID;
            if (!string.IsNullOrEmpty(qo.KeyWords))
                cond += " and Title like '%" + qo.KeyWords + "%' ";

            if (!string.IsNullOrEmpty(qo.Status))
                cond += " and Status = '" + qo.Status + "'";

            if (!string.IsNullOrEmpty(qo.StartDate))
                cond += " and CreateDate >='" + qo.StartDate + "'";
            if (!string.IsNullOrEmpty(qo.EndDate))
                cond += " and CreateDate <='" + qo.EndDate + " 23:59:59.999'";

            string orderby = GetNewsOrderby(qo.Sorts);

            List<News> listOfSource = dalNews.QueryListAsPage(cond, orderby, qo.CurPage, qo.PageSize, out qo.TotalCount);
            List<NewsInfo> listOfTarget = null;
            if (listOfSource != null)
            {
                Dictionary<int, string> dictCategory = QueryCategoryToHash();
                listOfTarget = new List<NewsInfo>();
                foreach (News source in listOfSource)
                {
                    NewsInfo target = new NewsInfo();
                    target.news = source;
                    target.CategoryName = dictCategory[source.CategoryID];
                    listOfTarget.Add(target);
                }
            }
            return listOfTarget;
        }
        #endregion

        #region "根据排序值返回资讯的排序语句； by 赵国伟 2012-03-31"
        /// <summary>
        /// 根据排序值返回资讯的排序语句；
        /// </summary>
        /// <param name="sorts"></param>
        /// <returns></returns>
        private string GetNewsOrderby(int sorts)
        {
            string orderby = "IsTop Desc,OrderValue, NewsID desc";
            switch (sorts)
            {
                case 1:
                    orderby = "CreateDate,NewsID desc ";
                    break;
                case 2:
                    orderby = "CreateDate desc,NewsID desc  ";
                    break;
                case 3:
                    orderby = "Clicks,NewsID desc ";
                    break;
                case 4:
                    orderby = "Clicks desc,NewsID desc";
                    break;
            }

            return orderby;
        }
        #endregion


        #region "查询指定类别下指定数量的资讯信息； by 赵国伟 2012-03-31"
        /// <summary>
        /// 查询指定类别下指定数量的资讯信息；
        /// </summary>
        /// <param name="top"></param>
        /// <param name="categoryId"></param>
        /// <param name="IsAvailable"></param>
        /// <returns></returns>
        public List<News> QueryNews(int top, int categoryId,bool IsAvailable)
        {
            string cond = "CategoryID=" + categoryId;
            if (IsAvailable)
                cond += " and Status='" + Status.正用.ToString() + "'";
            string orderby = "IsTop Desc,OrderValue, NewsID desc ";
            return dalNews.QueryList(top, cond, orderby);
        }
        #endregion




    }
}
